<?php
class Vat_lieu_xay_dung_model extends Model{

	var $_datagrid_name = 'dt1';
	
    function Vat_lieu_xay_dung_model(){
        parent::Model();       
    }
	function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('buildingmaterial');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,name,price,active');
    	$this->db->order_by('id DESC');
    	$query = $this->db->get('buildingmaterial');
    	$dts = $query->result_rowlist();
		for($i=0;$i<count($dts);$i++){
			$dts[$i][2] = $this->bsVndDot($dts[$i][2]);
		}
		
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$this->_deleteimage($id);
    		$this->db->delete('buildingmaterial',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			$this->_deleteimage($id[$i]);
	    		$this->db->delete('buildingmaterial',array('id'=>$id[$i]));
    		}
    	}
    }
    function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('product_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    		}    		    		
    		$categoryid 	= $this->input->post('cbo_category');
			$name 			= $this->input->post('txt_name');
			$price	 		= $this->input->post('txt_price');
			$unit  			= $this->input->post('txt_unit');
			$active			= FALSE!==$this->input->post('chk_active')?1:0;
			$supplier	  	= $this->input->post('txt_supplier');
			$description	= $this->input->post('txt_description');
			$this->db->insert('buildingmaterial',array('image'=>$image_name,'categoryid'=>$categoryid,'name'=>$name, 'price'=>$price, 'unit'=>$unit, 'description'=>$description, 'active'=>$active, 'supplier'=>$supplier));
    		redirect('vat_lieu_xay_dung');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('product_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    			$this->_deleteimage($this->input->post('hidden_id'));
    		}
    		if(!empty($image_name)){
    			$this->db->set('image',$image_name);
    		}
			$categoryid 	= $this->input->post('cbo_category');
			$name 			= $this->input->post('txt_name');
			$supplier   	= $this->input->post('txt_supplier');
			$price			= $this->input->post('txt_price');
			$unit			= $this->input->post('txt_unit');
			$description 	= $this->input->post('txt_description');
			$active			= FALSE!==$this->input->post('chk_active')?1:0;
			$this->db->where('id',$this->input->post('hidden_id'));
			$this->db->update('buildingmaterial',array('categoryid'=>$categoryid,'name'=>$name, 'supplier'=>$supplier, 'price'=>$price, 'description'=>$description, 'unit'=>$unit, 'active'=>$active));
			redirect('vat_lieu_xay_dung','location');
    	}
    }
    function getdetail($id){
    	$query = $this->db->get_where('buildingmaterial',array('id'=>$id));
    	if($query->num_rows){
    		$material = $query->row();
			$detail->id = $id;
    		$detail->categoryid 	= $material->categoryid;
			$detail->name 			= $material->name;
			$detail->supplier   	= $material->supplier;
			$detail->price			= $material->price;
			$detail->unit			= $material->unit;
			$detail->description 	= $material->description;
			$detail->image 			= $material->image;
			$detail->active			= $material->active;
	    	return $detail;
    	}
    	else return FALSE;
    } 
    function getcategorylist($type_id){
    	$this->db->order_by('sortorder,id');
    	$this->db->where('type',$type_id);
		$query = $this->db->get('buildingcategory');
    	$category = $query->result();
    	return $category;
    }   
	function _deleteimage($id){
    	$res = $this->db->get_where('news',array('id'=>$id));
    	$image = $res->row()->image;
    	if(file_exists($this->config->item('news_img_path').$image)){
    		@unlink($this->config->item('news_img_path').$image);
    	}
    }
	function bsVndDot($strNum){
		$len = strlen($strNum);
		$counter = 3;
		$result = "";
		while ($len - $counter >= 0)
			{
				$con = substr($strNum, $len - $counter , 3);
				$result = '.'.$con.$result;
				$counter+= 3;
			}
		$con = substr($strNum, 0 , 3 - ($counter - $len) );
		$result = $con.$result;
		if(substr($result,0,1)=='.'){
			$result=substr($result,1,$len+1);   
		}
		return $result;
	}
}

/* End of file */